package com;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: BGONZ8
 * Date: 9/24/11
 * Time: 12:13 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractTestCase implements ITestCase
{
    protected FeedService  feedService;
    protected TransactionFile txFile;
    protected DB db;
    protected String outputDir = "C:\\packages\\BNX\\Data\\transactionHistory\\";
    protected String result = "";
    protected String endpoint;
    protected String requestParameters;
    protected String reply = "";
    protected String postData = "";
    protected String userComment;
    protected List<String> sqlStmts;

    public AbstractTestCase()  throws Exception
    {
        db = new DB();
        sqlStmts = new ArrayList<String>();
    }

    public DB getDb() {
        return db;
    }

    public List<String> getSqlStmts() {
        return sqlStmts;
    }

    public FeedService getFeedService() {
        return feedService;
    }

    public void setFeedService(FeedService feedService) {
        this.feedService = feedService;
    }

    public TransactionFile getTxFile() {
        return txFile;
    }

    public void setTxFile(TransactionFile txFile) {
        this.txFile = txFile;
    }

    public String getResult() {
        return result;
    }

    public void copyTx() throws Exception
    {
        String txName = "C:\\packages\\BNX\\Data\\LockerTransactionFeed.Transaction.Sample.xml";
        PrintStream out = null;
        try {
            out = new PrintStream(new FileOutputStream(txName));
            out.print(txFile.toString());
        }
        finally {
            if (out != null) out.close();
        }
    }

    public void submit() throws Exception
    {
    }

    // ============================  REST ============================================

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getRequestParameters() {
        return requestParameters;
    }

    public void setRequestParameters(String requestParameters) {
        this.requestParameters = requestParameters;
    }

    public String getReply() {
        return reply;
    }

    // ============================  POST ============================================


    public String getPostData() {
        return postData;
    }

    public void setPostData(String postData) {
        this.postData = postData;
    }


    public String getUserComment() {
        return userComment;
    }

    public void setUserComment(String userComment) {
        this.userComment = userComment;
    }

    public Boolean validateSQL() throws Exception
    {
        try
        {
            for (String sqlQuery : this.getSqlStmts())
            {
                int ctr = 0;
                ResultSet rset = db.execute(sqlQuery);
                //Boolean first = rset.first();
                ResultSetMetaData rsmd = rset.getMetaData();
                int numColumns = rsmd.getColumnCount();

                result = result + "\n\n" + sqlQuery + "\n\n";
                while (rset.next() && ctr <= 10) {
                    for (int i=1; i<numColumns+1; i++) {
                        String columnName = rsmd.getColumnName(i);
                        result = result + "\t" + columnName + " = " + rset.getString(columnName) + "," ;
                    }
                    ctr++;
                    result = result + "\n\n";
                }
                if ( 0 == ctr )
                {
                    result = result + "\tRecord Not Found.\n\n";
                }
                else
                {
                    result = result + "\tRecord Found.\n\n";
                }
            }

        }
        catch (Exception e)
        {
            System.out.println("ERROR: " + e.getMessage());
            throw e;
        }
        finally
        {
            db.close();
        }
        return true;
    }
}
